package router

import (
	"embed"
	"github.com/gin-gonic/gin"
	"net/http"
	"wx_build/app"
	"wx_build/middleware"
	"wx_build/utils"
)

//go:embed css
var css embed.FS

//go:embed js
var js embed.FS

//go:embed fonts
var fonts embed.FS

//go:embed index.html
var index string

func RunServe() {
	r := gin.Default()
	//项目模块
	projectApi := new(app.ProjectApi)
	r.POST("/project/auto/build/:project_id", projectApi.AutoBuild) //自动构建
	r.Any("/css/*filepath", func(c *gin.Context) {
		staticServer := http.FileServer(http.FS(css))
		staticServer.ServeHTTP(c.Writer, c.Request)
	})
	r.Any("/js/*filepath", func(c *gin.Context) {
		staticServer := http.FileServer(http.FS(js))
		staticServer.ServeHTTP(c.Writer, c.Request)
	})
	r.Any("/fonts/*filepath", func(c *gin.Context) {
		staticServer := http.FileServer(http.FS(fonts))
		staticServer.ServeHTTP(c.Writer, c.Request)
	})
	r.GET("/", func(c *gin.Context) {
		c.Writer.WriteString(index)
	})
	userApi := new(app.UserApi)
	r.POST("/api/login", userApi.Login) //用户登录
	api := r.Group("/api")
	//校验登录
	api.Use(middleware.CheckLogin())
	api.POST("/is/login", userApi.IsLogin) //判断是否登录
	//用户管理模块
	api.POST("/create/user", userApi.CreateUser)        //创建用户
	api.POST("/user/list", userApi.List)                //查询用户列表
	api.POST("/user/delete", userApi.Delete)            //删除用户
	api.POST("/user/set/password", userApi.SetPassword) //修改用户密码

	//微信小程序模块
	wxAppApi := new(app.WxAppletApi)
	api.POST("/wx/applet/add", wxAppApi.Add)                //添加小程序
	api.POST("/wx/applet/update", wxAppApi.Update)          //编辑小程序
	api.POST("/wx/applet/list", wxAppApi.List)              //查询小程序列表
	api.POST("/wx/applet/delete", wxAppApi.Delete)          //删除小程序
	api.POST("/wx/applet/query/all", wxAppApi.QueryAll)     //查询所有小程序
	api.POST("/wx/applet/upload/file", wxAppApi.UploadFile) //文件上传

	api.POST("/project/add", projectApi.Add)                       //添加项目
	api.POST("/project/update", projectApi.Update)                 //编辑项目
	api.POST("/project/list", projectApi.List)                     //查询项目列表
	api.POST("/project/delete", projectApi.Delete)                 //删除项目
	api.POST("/project/get/branch/list", projectApi.GetBranchList) //查询分支列表
	api.POST("/project/build", projectApi.ProjectBuild)            //构建项目

	taskApi := new(app.TaskApi)
	api.POST("/task/list", taskApi.List) //查询构建列表
	port := utils.GetConfig("web_port").String()
	err := r.Run(port)
	if err != nil {
		panic(err)
	}
}
